<script setup>
import { ArrowDown, ArrowUp } from '@element-plus/icons-vue'
import { ref,onMounted } from 'vue'
import Info from './info.vue'
import {deleteApi, deleteBatchApi, pageApi, updateRoleApi} from "../../../api/user";
import {allApi} from "../../../api/role"
import {ElMessage, ElMessageBox} from "element-plus";

// 表单状态
const filterState = ref(true)
// 表单数据 查询
const isExpand = ref(false)
const formData = ref({
  page: 1,
  size: 10,
  name: '',
  no: ''
})
const tableData=ref({})
onMounted(()=>{
  loadData()
})
// 分页
const onSizeChange = (val) => {
  loadData()
}
const onCurrentChange = (val) => {
  loadData()
}
//查询列表
const loadData=async ()=>{
  const r = await pageApi(formData.value)
  tableData.value=r.data;
}
// 用户编辑
const infoRef = ref()
const onUserInfo = (row) => {
  infoRef.value.state = true

  if (row) {
    infoRef.value.formData.id = row.id
    infoRef.value.formData.no = row.no
    infoRef.value.formData.username = row.username
  }
}

// 用户删除
const onDelete = (row) => {
  ElMessageBox.confirm('确定删除该条数据吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
      .then(async () => {
       const  r=await deleteApi({id:row.id});
       if(r.code==0){
            ElMessage({
              type: 'success',
              message: '删除成功'
            })
            loadData()
          }else {
            ElMessage({
              type: 'info',
              message: r.data
            })
          }
        })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消'
        })
      })
}

// 批量选择
const objs = ref([])
const onSelectionChange = (val) => {
  objs.value = val
}
const onDeleteBatch=async ()=>{
  //组装对象
  let ids=new Array();
  for (let i = 0; i < objs.value.length; i++) {
    ids[i]=objs.value[i].id
  }
  //请求接口 批量删除
  const r=await deleteBatchApi(ids)
  if(r.code==0){
    ElMessage({
      type: 'success',
      message: '批量删除成功！'
    })
    loadData()
  }else {
    ElMessage.error(r.data)
  }
}
const userRole=ref({})
const setrole=ref(false)
const roles=ref([])
const rids=ref([])
const onAssignRole= async (row)=>{
  userRole.value.uid=row.id;
  //加载 所有角色
  const r=await allApi({uid:row.id});
  //所有角色
  roles.value=r.data.roles;
  //用户当前的角色
  rids.value=r.data.rids;
  //显示
  setrole.value=true;
}
// 操作El-tree
const tree=ref({})
//分配对应的角色
const onChangeRole=async ()=>{
  // 目前被选中的部门节点
  userRole.value.rids = tree.value.getCheckedKeys();
  const r=await updateRoleApi(userRole.value);
    if(r.code==0){
      //成功
      setrole.value=false;
      ElMessage({
        type: 'success',
        message: '用户角色分配成功'
      })
    }else {
      ElMessage({
        type: 'info',
        message: r.data
      })
    }
}

</script>
<template>
  <Box>
    <!-- 逻辑层  父传子-->
    <Info ref="infoRef"  @flush="loadData" />
    <!-- 逻辑层 -->

    <!-- 筛选 -->
    <template #filter v-if="filterState">
      <el-form :inline="true" :model="formData">
        <el-form-item label="工号">
          <el-input placeholder="请输入工号" v-model="formData.no" />
        </el-form-item>
        <el-form-item label="用户名">
          <el-input placeholder="请输入用户名" v-model="formData.name" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary"> <span class="iconfont icon-sousuo"></span>搜索 </el-button>
          <el-button type="default"> 重置 </el-button>
          <el-button type="primary" link @click="isExpand = !isExpand">
            {{ isExpand ? '收起' : '展开' }}
            <el-icon>
              <ArrowUp v-if="isExpand" />
              <ArrowDown v-else />
            </el-icon>
          </el-button>
        </el-form-item>
      </el-form>
    </template>
    <!-- 筛选 -->
    <!-- 内容 -->
    <!-- 按钮组 -->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="onUserInfo">
          <span class="iconfont icon-jiahao"></span>添加
        </el-button>
        <el-button type="danger" :disabled="!objs.length" @click="onDeleteBatch">
          <span class="iconfont icon-shanchu"></span>批量删除
        </el-button>
      </el-col>

      <el-col :span="12" style="text-align: right">
        <el-button circle @click="filterState = !filterState">
          <span class="iconfont icon-sousuo"></span>
        </el-button>
        <el-button circle>
          <span class="iconfont icon-shuaxin"></span>
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮组 -->
    <!-- 表格 -->
    <el-table :data="tableData.data" :border="true" @selection-change="onSelectionChange">
      <el-table-column type="selection" align="center" fixed="left"></el-table-column>
      <el-table-column label="序号" prop="id" align="center"></el-table-column>
      <el-table-column label="工号" prop="no" align="center"></el-table-column>
      <el-table-column label="用户名" prop="username" align="center"></el-table-column>
      <el-table-column label="创建时间" prop="ctime" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template #default="scoped">
          <el-button type="primary" link size="small" @click="onUserInfo(scoped.row)">
            <span class="iconfont icon-bianji"></span>修改
          </el-button>
          <el-button type="primary" link size="small" @click="onDelete(scoped.row)">
            <span class="iconfont icon-shanchu"></span>删除
          </el-button>
          <el-button type="primary" link size="small" @click="onAssignRole(scoped.row)">
            <span class="iconfont icon-gengduo"></span>
            分配角色
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="formData.page"
      v-model:page-size="formData.size"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
    />
    <!-- 分页 -->
    <!-- 内容 -->
<!--对用户分配角色-->
    <el-dialog
        v-model="setrole"
        :title="`用户角色分配`"
        width="600"
        :before-close="() => (setrole = false)"
    >
      <!-- 内容 -->
      <el-form
          size="large"
          label-width="80"
      >
        <el-form-item label="角色列表">
          <el-tree
              :data="roles"
              ref="tree"
              node-key="id"
              :default-checked-keys="rids"
              :props="{
            label: 'name',
            value: 'id',
            }" show-checkbox></el-tree>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="onChangeRole">确定分配</el-button>
          <el-button type="default" @click="state = false">取消</el-button>
        </el-form-item>
      </el-form>
      <!-- 内容 -->
    </el-dialog>
  </Box>
</template>
